/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"
#include "myassert.h"
#include "TestSession.h"
#include "TestEventServer.h"
#include "TestRoom.h"

#include "MudCreature.h"
#include "MudExit.h"
#include "MudItem.h"
#include "MudObject.h"
#include "MudPlayer.h"
#include "Parser.h"
#include "ParserFactory.h"
#include "color.h"
#include "Area.h"

#include "Commands.h"

using namespace std;
using namespace Chronos;


class RoomCommandsTest {
public:
    RoomCommandsTest()
		: area(0, "")
        , parser(*ParserFactory::create())
		, room("RoomName")
        , session("player")
        , player(session.getPlayer())
    {
		room.addPlayer(player, "");
        session.clear();
        room.clear();
    }
    ~RoomCommandsTest() {
		room.removePlayer(player, "");
        delete &parser;
    }
	void checkResult(const char* input, const string& expected) {
		parser.parse(session, input);
		session.SESSION_CHECKWRITTEN(expected);
	}

    Area area;
    Parser& parser;
    TestRoom room;

    TestSession session;
    MudPlayer& player;
};


TEST(RoomCommandsTest, testLookCommand) {
	checkResult("look up", "Usage: look\n");

	checkResult("look", "RoomName\nroomDescr\n");

    room.addObject(new MudObject("invisible", ""));
    room.findObject("invisible")->setVisible(false);
	checkResult("look", "RoomName\nroomDescr\n");

    room.addObject(new MudObject("testObject", ""));
	checkResult("look", "RoomName\nroomDescr\nYou also see:\n  testObject\n");

    room.addObject(new MudCreature("creature", "", 1, 1, 1, 1));
	checkResult("look", "RoomName\nroomDescr\nYou also see:\n  testObject\n  creature\n");

	room.addObject(new MudEastExit(area, 2));
	checkResult("look", "RoomName ("ANSI_YELLOW"east"ANSI_NORMAL")\nroomDescr\nYou also see:\n  testObject\n  creature\n");
}


TEST(RoomCommandsTest, testLookAtCommand) {
	checkResult("look at", "Usage: look at [object]\n");
	checkResult("look at notExist", "There is no notExist here.\n");
	checkResult("look at player", "You look at yourself.\n");

    room.addObject(new MudObject("testObject", "testDescription"));
	checkResult("look at testObject", "testDescription\n");

	player.addItem(new MudItem(0, "testItem", "testItemDescr", 1));
	checkResult("look at testItem", "testItemDescr\n");
}


TEST(RoomCommandsTest, testGreetCommand) {
	TestSession session2("player2");
	room.addPlayer(session2.getPlayer(), "");
	session.clear();
	session2.clear();

	checkResult("greet", "Usage: greet [player]\n");
	checkResult("greet player", "You greet yourself, hello\n");	
	checkResult("greet nonExist", "There is no nonExist here.\n");

	room.addObject(new MudObject("testObject", ""));
	checkResult("greet testObject", "You cannot greet that.\n");

	checkResult("greet player2", "You greet player2\n");
	session2.SESSION_CHECKWRITTEN("player greets you.\n");

	room.removePlayer(session2.getPlayer(), "");
}


TEST(RoomCommandsTest, testSayCommand) {
	checkResult("say", "Usage: say [msg]\n");
	checkResult("say hello world", "You say: 'hello world'\n");
	room.ROOM_CHECKWRITTEN("player says: 'hello world'\n");
}

